home *** CD-ROM | disk | FTP | other *** search
- /*
- ThreadedAppShell.c
- copyright (c)1999 Ben Martz
- */
-
- /* includes */
- #include <Threads.h>
- #include "malloc.h"
- #include "42.h"
- #include "42_plugin_manager.h"
- #include "42_plugin_life.h"
- #include "42_plugin_ball.h"
-
- /* resource ids */
- #define kMenuBarID 128
- #define mApple 128
- #define iAbout 1
- #define mFile 129
- #define iQuit 1
- #define mEdit 130
- #define kWindowID 128
- #define kAboutID 128
- #define kErrorID 129
-
- /* prototypes */
- void InitToolbox(void);
- void InitEnvironment(void);
- void InitThread(void *entry);
- void EventThread(void);
- int HandleDialogEvent(EventRecord *event);
- void HandleMouseDownEvent(EventRecord *event);
- void HandleUpdateEvent(EventRecord *event);
- void HandleMenuChoice(long choice);
- void HandleAEQuit(void);
- void ExitError(unsigned char *msg);
- void SetTextBox(short item, unsigned char *str);
- void CheckTextBox(short item);
- void SetControlFont(short item);
-
- /* globals */
- Boolean gDone = false;
- DialogPtr gMainDialog;
-
- /* main entry */
- int main(void)
- {
- InitToolbox();
- InitEnvironment();
- InitThread(EventThread);
- InitThread(forty_two_thread);
-
- while(!gDone)
- YieldToAnyThread();
-
- return 0;
- } /* main */
-
- /* the macintosh toolbox is our friend! */
- void InitToolbox(void)
- {
- InitGraf(&qd.thePort);
- InitFonts();
- FlushEvents(everyEvent,0L);
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs(nil);
- InitCursor();
-
- MaxApplZone();
- MoreMasters();
- } /* InitToolbox */
-
- /* set up our little world that we live in */
- void InitEnvironment(void)
- {
- Handle mbar;
- MenuHandle applemenu;
- int i;
-
- mbar = GetNewMBar(kMenuBarID);
- if(!mbar)
- ExitError("\pCan't load menu bar!");
- SetMenuBar(mbar);
-
- applemenu = GetMenuHandle(mApple);
- AppendResMenu(applemenu,'DRVR');
- InsertMenu(applemenu, 0);
-
- DrawMenuBar();
-
- gMainDialog = GetNewDialog(130,0,(WindowPtr)-1L);
- if(!gMainDialog)
- ExitError("\pcan't load dialog");
-
- SetPort(gMainDialog);
- TextFont(1);
- TextSize(9);
- for(i = 0; i < last_item; i++)
- SetControlFont(i);
- ShowWindow(gMainDialog);
-
- DrawDialog(gMainDialog);
-
- AEInstallEventHandler(kCoreEventClass,kAEQuitApplication,(AEEventHandlerUPP)HandleAEQuit,0,false);
- } /* InitEnvironment */
-
- /* set up a new thread in our world */
- void InitThread(void *entry)
- {
- if(NewThread(kCooperativeThread,(ThreadEntryProcPtr)entry,nil,0,kCreateIfNeeded,nil,nil))
- ExitError("\pUnable to create thread!");
- } /* InitThread */
-
- /* main event dispatch */
- void EventThread(void)
- {
- EventRecord event;
-
- while(!gDone)
- {
- if(WaitNextEvent(everyEvent,&event,0,nil))
- {
- if(IsDialogEvent(&event))
- {
- if(HandleDialogEvent(&event)) continue;
- }
- switch(event.what) {
- case keyDown:
- if(event.modifiers & cmdKey)
- HandleMenuChoice(MenuKey(event.message & charCodeMask));
- break;
- case autoKey:
- break;
- case mouseDown:
- HandleMouseDownEvent(&event);
- break;
- case activateEvt:
- case updateEvt:
- HandleUpdateEvent(&event);
- break;
- case kHighLevelEvent:
- AEProcessAppleEvent(&event);
- break;
- } /* switch */
- } /* if */
-
- YieldToAnyThread();
- } /* while */
- } /* EventThread */
-
- int HandleDialogEvent(EventRecord *event)
- {
- DialogPtr dialog;
- short item;
-
- if(event->modifiers & cmdKey)
- {
- HandleMenuChoice(MenuKey(event->message & charCodeMask));
- return 1;
- }
-
- if(FrontWindow() != gMainDialog) return 0;
-
- if(DialogSelect(event, &dialog, &item))
- {
- switch(item)
- {
- case start:
- life_ipc(LIFE_START,0);
- break;
- case stop:
- life_ipc(LIFE_STOP,0);
- break;
- case step:
- life_ipc(LIFE_STEP,0);
- break;
- case random:
- life_ipc(LIFE_LOAD,5);
- break;
- case pattern_1:
- case pattern_2:
- case pattern_3:
- case pattern_4:
- life_ipc(LIFE_LOAD,item - pattern_1 + 1);
- break;
- case color_c:
- life_ipc(LIFE_SETCOLOR,0x0000FFFF);
- ball_ipc(BALL_COLOR,0x00FFFF00);
- break;
- case color_y:
- life_ipc(LIFE_SETCOLOR,0x00FFFF00);
- ball_ipc(BALL_COLOR,0x00FF00FF);
- break;
- case color_m:
- life_ipc(LIFE_SETCOLOR,0x00FF00FF);
- ball_ipc(BALL_COLOR,0x00000000);
- break;
- case color_k:
- life_ipc(LIFE_SETCOLOR,0x00000000);
- ball_ipc(BALL_COLOR,0x0000FFFF);
- break;
- case ticker_buy:
- SysBeep(10);
- break;
- default:
- GlobalToLocal(&event->where);
- forty_two_click(&event->where);
- break;
- }
- return 1;
- }
- else
- return 0;
- } /* HandleDialogEvent */
-
- /* handle mousedown events */
- void HandleMouseDownEvent(EventRecord *event)
- {
- WindowPtr wind;
- short part;
- Rect rect = {-4096,-4096,4096,4096};
-
- part = FindWindow(event->where, &wind);
-
- switch(part)
- {
- case inMenuBar:
- HandleMenuChoice(MenuSelect(event->where));
- break;
- case inSysWindow:
- SystemClick(event, wind);
- case inContent:
- if(wind != FrontWindow())
- SelectWindow(wind);
- if(wind == gMainDialog)
- {
- GlobalToLocal(&event->where);
- forty_two_click(&event->where);
- }
- break;
- case inDrag:
- DragWindow(wind, event->where,&rect);
- break;
- case inGoAway:
- if(TrackGoAway(wind,event->where))
- gDone = true;
- break;
- } /* switch */
- } /* HandleMouseDownEvent */
-
- /* handle window updates */
- void HandleUpdateEvent(EventRecord *event)
- {
- WindowPtr wind;
- RgnHandle clip,oldclip;
-
- wind = (WindowPtr) event->message;
-
- BeginUpdate(wind);
- DrawDialog(gMainDialog);
- EndUpdate(wind);
-
- DrawDialog(gMainDialog);
- forty_two_update_all();
- } /* HandleUpdateEvent */
-
- /* handle a menu selection or command key */
- void HandleMenuChoice(long choice)
- {
- short menu = HiWord(choice);
- short item = LoWord(choice);
- MenuHandle applemenu;
- Str255 accname;
-
- switch(menu)
- {
- case mApple:
- switch(item)
- {
- case iAbout:
- Alert(kAboutID,nil);
- break;
- default:
- applemenu = GetMenuHandle(mApple);
- GetMenuItemText(applemenu,item,accname);
- OpenDeskAcc(accname);
- } /* switch */
- break;
- case mFile:
- switch(item)
- {
- case iQuit:
- gDone = true;
- break;
- } /* switch */
- break;
- case mEdit:
- break;
- } /* switch */
-
- HiliteMenu(0);
- } /* HandleMenuChoice */
-
- /* we are a peaceful application */
- void HandleAEQuit(void)
- {
- gDone = true;
- } /* HandleAEQuit */
-
- /* display an error dialog and quit */
- void ExitError(unsigned char *msg)
- {
- ParamText(msg,"\p","\p","\p");
- Alert(kErrorID,nil);
- ExitToShell();
- } /* ExitError */
-
- void SetTextBox(short item, unsigned char *str)
- {
- short t;
- Handle h;
- Rect r;
-
- GetDialogItem(gMainDialog,item,&t,&h,&r);
- if(h)
- {
- SetDialogItemText(h,str);
- }
- }
-
- void CheckTextBox(short item)
- {
- short t;
- Handle h;
- Rect r;
- Str255 str;
-
- GetDialogItem(gMainDialog,item,&t,&h,&r);
- if(h)
- {
- GetDialogItemText(h,str);
- if(str[0] > 7) str[0] = 7;
- SetDialogItemText(h,str);
- }
- }
-
- void SetControlFont(short item)
- {
- short t;
- Handle h;
- Rect r;
- ControlFontStyleRec style;
-
- GetDialogItem(gMainDialog,item,&t,&h,&r);
- style.flags = kControlUseFontMask | kControlUseSizeMask | kControlUseJustMask;
- style.font = 1;
- style.size = 9;
- style.just = 1;
- SetControlFontStyle((ControlHandle)h,&style);
- }